﻿using Gumini.JulaDB.Enums;
using Gumini.JulaDB.Provider;
using Gumini.JulaDB.Test.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Gumini.JulaDB.Test
{
    /// <summary>
    /// A set of tests for the INSERT statement.
    /// </summary>
    [TestClass]
    public class InsertTest
    {
        private static JulaDbConnection _connection;
        private static PersonTestDataCreator _peopleData;

        [TestInitialize]
        public void InitializeTest()
        {
            // database is initialized before every test to discard any changes made by previous tests
            _connection = TestUtils.CreateTestDatabase();
            _peopleData = new PersonTestDataCreator(_connection);
        }

        [TestCleanup]
        public void CleanupTest()
        {
            _connection.Close();
        }

        /// <summary>
        /// Insert a row specifying all column names.
        /// </summary>
        [TestMethod]
        public void InsertFullColumnList()
        {
            _connection.ExecuteValidNonQuery("insert into person (Id, FirstName, LastName) values (14, 'Jan', 'Nowak')");
        }

        /// <summary>
        /// Insert a row specifying only a single column.
        /// </summary>
        [TestMethod]
        public void InsertPartialColumnList()
        {
            _connection.ExecuteValidNonQuery("insert into person (LastName) values ('Nowak')");
        }

        /// <summary>
        /// Insert a row specifying a single column. The value is an expression that needs to be evaluated first.
        /// </summary>
        [TestMethod]
        public void InsertExpression()
        {
            _connection.ExecuteValidNonQuery("insert into person (FirstName) values ('Ja' || 'n' || (2 + 3))");
        }

        /// <summary>
        /// Attempt to insert to a nonexistent column should fail with an error.
        /// </summary>
        [TestMethod]
        public void InsertUnknownColumn()
        {
            _connection.ExecuteInvalidCommand("insert into person (Company) values ('The Company')", ErrorCode.UnknownIdentifier);
        }

        /// <summary>
        /// INSERT statement with different number of column names and values. This should return an error.
        /// </summary>
        [TestMethod]
        public void InsertColumnMismatch()
        {
            _connection.ExecuteInvalidCommand("insert into person (Id, FirstName, LastName) values (12, 'Anna')", ErrorCode.NumberOfColumnsDoesNotMatch);
        }
    }
}
